package org.ala.tiktools.generator.mybatis.creator.mapper_shard;

import freemarker.template.TemplateException;
import org.ala.tiktools.generator.mybatis.config.info.Part;
import org.ala.tiktools.generator.mybatis.creator.AbstractCreator;
import org.ala.tiktools.generator.mybatis.creator.dao.DaoInfo;
import org.ala.tiktools.generator.mybatis.creator.entity.EntityInfo;
import org.ala.tiktools.generator.mybatis.creator.mapper.MapperInfo;
import org.ala.tiktools.generator.mybatis.dbinfo.TableInfo;
import org.ala.tiktools.generator.mybatis.tools.Tools;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ala
 * @date 2025-01-08 15:51
 */
public class MapperShardCreator extends AbstractCreator {


    public MapperInfo create(Part p, TableInfo t, EntityInfo e, DaoInfo d) throws TemplateException, IOException {
        MapperInfo m = MapperInfo.build(p, t, e, d);
        Map<String, Object> dataMeta = new HashMap<>();
        dataMeta.put("m", m);

        String out = m.getPkg();
        out = this.getOutRootPath() + out.replaceAll("\\.", "/");
        Tools.existsAndMakeDirs(out);
        out = out + "/" + m.getXmlName() + ".xml";

        output("mapper-shard.ftl", dataMeta, out);

        System.out.println("生成mapper.xml. path:" + out);
        return m;
    }
}
